package com.fz.song.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.fz.song.pojo.BlogLabel;
import com.fz.song.pojo.BlogType;
import com.fz.song.service.IBlogLableService;
import com.fz.song.service.IBlogTypeService;
import com.fz.song.util.DateUtil;
import com.fz.song.util.ResultObj;
import com.fz.song.util.UUIDUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("bloglable")
public class BlogLableController {


    @Autowired
    private IBlogLableService blogLableService;

    @RequestMapping("blog_lable_list_vlew.html")
    public String blog_lable_list_vlew_html() {
        return "admin/bloglable/blog_lable_list";
    }

    @RequestMapping("blog_lable_list_data")
    public String blog_lable_list_data(HttpServletRequest request, Integer page, @RequestParam(value = "limit", required = false, defaultValue = "10") int limit) {
        //获取数据
        PageInfo<BlogLabel> pageInfo = blogLableService.getBlogLableListData(page, limit);
        request.setAttribute("pageInfo", pageInfo);
        return "admin/bloglable/blog_lable_list_data";
    }

    @RequestMapping("go_bloglable_saveorupdate.html")
    public String go_bloglable_saveorupdate_html(HttpServletRequest request, String id) {
        if (null != id && !"".equals(id)) {
            BlogLabel blogLabel = blogLableService.getBlogLableByid(id);
            request.setAttribute("bloglabel", blogLabel);
        }

        return "admin/bloglable/blog_lable_save_or_update";
    }

    @RequestMapping("save_bloglable")
    @ResponseBody
    public Object save_bloglable(HttpServletRequest request, String jsonstr) {
        BlogLabel blogLabel = JSONObject.parseObject(jsonstr, BlogLabel.class);
        if (blogLabel.getId() == null || "".equals(blogLabel.getId())) {
            blogLabel.setId(UUIDUtil.getUUID());
            blogLabel.setCreatetime(DateUtil.getDateNow());
            blogLabel.setUpdatetime(DateUtil.getDateNow());
            blogLabel.setEffective(0);
            blogLabel.setDel(0);
            return blogLableService.saveBlogLabel(blogLabel);
        } else {
            BlogLabel blogType_db = blogLableService.getBlogLableByid(blogLabel.getId());
            blogType_db.setUpdatetime(DateUtil.getDateNow());
            blogType_db.setLabelName(blogLabel.getLabelName());
            return blogLableService.updateBlogLabel(blogLabel);
        }
    }


    @RequestMapping("updateStatus")
    @ResponseBody
    public Object updateStatus(HttpServletRequest request, String id) {
        BlogLabel blogLabel_db = blogLableService.getBlogLableByid(id);
        Integer effective = blogLabel_db.getEffective();
        if (effective == 1) {
            blogLabel_db.setEffective(0);
        } else {
            blogLabel_db.setEffective(1);
        }
        ResultObj resultObj = blogLableService.updateBlogLabel(blogLabel_db);
        return resultObj;
    }
    @RequestMapping("delBlogLable")
    @ResponseBody
    public Object delBlogLable(HttpServletRequest request, String id) {
        ResultObj resultObj = blogLableService.delBlogLabel(id);
        return resultObj;
    }
}
